package tt.dz.biz.service.account;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import tt.dz.common.BigDecimalUtil;
import tt.dz.vo.MerchAccountDividendVO;

public final class AccountDividendSupport
{
  public final List<MerchAccountDividendVO> mergeMerchAccountDividend(List<MerchAccountDividendVO> paramList)
  {
    if (CollectionUtils.isEmpty(paramList)) {
      return paramList;
    }
    Map<MerchAccDivKey, MerchAccountDividendVO> merchAccDivMap = new HashMap();
    MerchAccDivKey key = null;
    MerchAccountDividendVO value = null;
    for (MerchAccountDividendVO vo : paramList)
    {
      key = MerchAccDivKey.instane(vo);
      value = (MerchAccountDividendVO)merchAccDivMap.get(key);
      if (null == value) {
        value = vo;
      } else {
        value = merge(value, vo);
      }
      merchAccDivMap.put(key, value);
    }
    return new ArrayList(merchAccDivMap.values());
  }
  
  private MerchAccountDividendVO merge(MerchAccountDividendVO value, MerchAccountDividendVO vo)
  {
    if (value == null) {
      return vo;
    }
    if (vo == null) {
      return value;
    }
    value.setAmount(BigDecimalUtil.add(value.getAmount(), vo.getAmount()));
    String rvalue = StringUtils.defaultString(value.getRemark(), "");
    String rvo = StringUtils.defaultString(vo.getRemark(), "");
    value.setRemark(rvalue + "   " + rvo);
    return value;
  }
  
  protected static class MerchAccDivKey
  {
    private String merchantCode;
    private String subjectCode;
    private String subSubjectCode;
    private String merchantCode1;
    private Integer loanFlag;
    private String tradeNo;
    
    protected MerchAccDivKey() {}
    
    protected MerchAccDivKey(String merchantCode, String subjectCode, String subSubjectCode, String merchantCode1, Integer loanFlag, String tradeNo)
    {
      this.merchantCode = merchantCode;
      this.subjectCode = subjectCode;
      this.subSubjectCode = subSubjectCode;
      this.merchantCode1 = merchantCode1;
      this.loanFlag = loanFlag;
      this.tradeNo = tradeNo;
    }
    
    protected static MerchAccDivKey instane(MerchAccountDividendVO vo)
    {
      MerchAccDivKey key = new MerchAccDivKey(vo.getMerchantCode(), vo.getSubjectCode(), vo.getSubSubjectCode(), vo.getMerchantCode1(), vo.getLoanFlag(), vo.getTradeNo());
      return key;
    }
    
    public String getMerchantCode()
    {
      return this.merchantCode;
    }
    
    public void setMerchantCode(String merchantCode)
    {
      this.merchantCode = merchantCode;
    }
    
    public String getSubjectCode()
    {
      return this.subjectCode;
    }
    
    public void setSubjectCode(String subjectCode)
    {
      this.subjectCode = subjectCode;
    }
    
    public String getSubSubjectCode()
    {
      return this.subSubjectCode;
    }
    
    public void setSubSubjectCode(String subSubjectCode)
    {
      this.subSubjectCode = subSubjectCode;
    }
    
    public String getMerchantCode1()
    {
      return this.merchantCode1;
    }
    
    public void setMerchantCode1(String merchantCode1)
    {
      this.merchantCode1 = merchantCode1;
    }
    
    public Integer getLoanFlag()
    {
      return this.loanFlag;
    }
    
    public void setLoanFlag(Integer loanFlag)
    {
      this.loanFlag = loanFlag;
    }
    
    public void setTradeNo(String tradeNo)
    {
      this.tradeNo = tradeNo;
    }
    
    public String getTradeNo()
    {
      return this.tradeNo;
    }
    
    public boolean equals(Object o)
    {
      if (this == o) {
        return true;
      }
      if ((o == null) || (getClass() != o.getClass())) {
        return false;
      }
      MerchAccDivKey that = (MerchAccDivKey)o;
      if (this.merchantCode != null ? !this.merchantCode.equals(that.merchantCode) : that.merchantCode != null) {
        return false;
      }
      if (this.subjectCode != null ? !this.subjectCode.equals(that.subjectCode) : that.subjectCode != null) {
        return false;
      }
      if (this.subSubjectCode != null ? !this.subSubjectCode.equals(that.subSubjectCode) : that.subSubjectCode != null) {
        return false;
      }
      if (this.merchantCode1 != null ? !this.merchantCode1.equals(that.merchantCode1) : that.merchantCode1 != null) {
        return false;
      }
      if (this.loanFlag != null ? !this.loanFlag.equals(that.loanFlag) : that.loanFlag != null) {
        return false;
      }
      return that.tradeNo == null ? true : this.tradeNo != null ? this.tradeNo.equals(that.tradeNo) : false;
    }
    
    public int hashCode()
    {
      int result = this.merchantCode != null ? this.merchantCode.hashCode() : 0;
      result = 31 * result + (this.subjectCode != null ? this.subjectCode.hashCode() : 0);
      result = 31 * result + (this.subSubjectCode != null ? this.subSubjectCode.hashCode() : 0);
      result = 31 * result + (this.merchantCode1 != null ? this.merchantCode1.hashCode() : 0);
      result = 31 * result + (this.loanFlag != null ? this.loanFlag.hashCode() : 0);
      result = 31 * result + (this.tradeNo != null ? this.tradeNo.hashCode() : 0);
      return result;
    }
    
    public String toString()
    {
      return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE);
    }
  }
}
